package com.yuanzong.wuliu.service.admin.business.customer;

import com.yuanzong.wuliu.common.exception.BusinessException;
import com.yuanzong.wuliu.common.utils.id.SnowFlakeUtils;
import com.yuanzong.wuliu.common.vo.ApiResult;
import com.yuanzong.wuliu.common.vo.ApiResultPage;
import com.yuanzong.wuliu.controller.admin.business.customer.param.AdminBusinessCustomerAddParam;
import com.yuanzong.wuliu.controller.admin.business.customer.param.AdminBusinessCustomerEnableOrDisableParam;
import com.yuanzong.wuliu.controller.admin.business.customer.param.AdminBusinessCustomerModifyParam;
import com.yuanzong.wuliu.controller.admin.business.customer.param.AdminBusinessCustomerPageParam;
import com.yuanzong.wuliu.controller.intercdeptor.AdminHandlerInterceptorAdapter;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomer;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerPageQuery;
import com.yuanzong.wuliu.model.system.user.SystemUser;
import com.yuanzong.wuliu.service.business.customer.factory.BusinessCustomerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AdminBusinessCustomerService {

    public ApiResult page(AdminBusinessCustomerPageParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerPageQuery businessCustomerPageQuery = new BusinessCustomerPageQuery();
        BeanUtils.copyProperties(param, businessCustomerPageQuery);
        ApiResultPage<BusinessCustomer> apiResultPage = BusinessCustomerFactory.getBusinessCustomerService().pageSubject(businessCustomerPageQuery);
        return ApiResult.success(apiResultPage);
    }

    public ApiResult add(AdminBusinessCustomerAddParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        BusinessCustomer businessCustomer = BusinessCustomerFactory.getBusinessCustomerService().queryByPhone(param.getPhone());
        if (businessCustomer != null) {
            throw new BusinessException("手机号已存在");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        businessCustomer = new BusinessCustomer();
        businessCustomer.setId(SnowFlakeUtils.getId());
        businessCustomer.setPhone(param.getPhone());
        businessCustomer.setNickname(param.getNickname());
        businessCustomer.setStatus(1);
        businessCustomer.setCreateTime(LocalDateTime.now());
        businessCustomer.setCreateUserId(runtimeUser.getId());
        businessCustomer.setCreateUserNickname(runtimeUser.getNickname());
        BusinessCustomerFactory.getBusinessCustomerService().save(businessCustomer);
        return ApiResult.success();
    }

    public ApiResult modify(AdminBusinessCustomerModifyParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        BusinessCustomer businessCustomer = BusinessCustomerFactory.getBusinessCustomerService().getById(param.getId());
        if (businessCustomer == null) {
            throw new BusinessException("客户不存在");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        businessCustomer.setNickname(param.getNickname());
        BusinessCustomerFactory.getBusinessCustomerService().updateById(businessCustomer);
        return ApiResult.success();
    }

    public ApiResult enableOrDisable(AdminBusinessCustomerEnableOrDisableParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        SystemUser runtimeUser = AdminHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数校验
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        BusinessCustomer businessCustomer = BusinessCustomerFactory.getBusinessCustomerService().getById(param.getId());
        if (businessCustomer == null) {
            throw new BusinessException("客户不存在");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        if (businessCustomer.getStatus().equals(1)) {
            businessCustomer.setStatus(0);
        } else {
            businessCustomer.setStatus(1);
        }
        BusinessCustomerFactory.getBusinessCustomerService().updateById(businessCustomer);
        return ApiResult.success();
    }
}
